/**
 * 
 */
package com.shine.icis.dao.imp;

import java.util.List;

import org.hibernate.Query;

import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.sql.hibernate.CommQuery;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.icis.dao.IMetadataCheckLogDAO;
import com.shine.icis.dao.imp.BaseHibernateDAO;
import com.shine.icis.hbean.MetadataCheckItem;
import com.shine.icis.hbean.MetadataCheckLog;
import com.shine.icis.hbean.ProgExeLog;

/**
 * 元数据质量检查日志的查询
 * @author zhangxr  2010-10-22
 * */

public class MetadataCheckLogDAO extends BaseHibernateDAO implements
		IMetadataCheckLogDAO {

	/**
	 * 元数据质量检查日志类别，状态为启用
	 * @author zhangxr  2010-10-22
	 * */
	@SuppressWarnings("unchecked")
	@Override
	public List<MetadataCheckItem> findMetadataCheckItems(char enableFlag) {
		String sql = null;
		if (enableFlag == MetadataCheckItem.ENABLE) {
			sql = "from MetadataCheckItem i where i.enableFlag<>:enableFlag order by i.itemID";
			enableFlag = MetadataCheckItem.DISABLE;
		} else {
			sql = "from MetadataCheckItem i where i.enableFlag=:enableFlag order by i.itemID";
		}
		Query query = this.getSession().createQuery(sql);
		query.setCharacter("enableFlag", enableFlag);
		return query.list();
//		List<DictOption> ops = new ArrayList<DictOption>(val.size());
//		for (MetadataCheckItem item : val) {
//			DictOption op = new DictOption();
//			op.setKey(String.valueOf(item.getItemID()));
//			op.setText(item.getItemName());
//			ops.add(op);
//		}
//		return ops;
	}
	
	/**
	 * 查询元数据质量检查日志
	 * @author zhangxr  2010-10-22
	 * @param qp
	 * */
	@Override
	public ListPage<MetadataCheckLog> findMetadataCheckLogPage(
			QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException("非法参数:查询参数对象为null");
		}
		try {
			StringBuffer sbQueryString = new StringBuffer();			
			sbQueryString.append("select l from MetadataCheckLog as l where 1=1");
			if (qp.getParameter("operDate") != null) {
				sbQueryString.append(" and l.operDate=:operDate ");
			}
			if (qp.getParameter("checkItemID") != null) {
				sbQueryString.append(" and l.metadataCheckItem.itemID = :checkItemID ");
			}
			if (qp.getParameter("prepLevelID") != null) {
				sbQueryString.append(" and l.prepLevelID=:prepLevelID ");
			}
			if(qp.getParameter("runStatus") != null){
				sbQueryString.append(" and l.runStatus=:runStatus");
			}
			Long startLogID = (Long)qp.getParameter("startLogID");
			if(startLogID != null){
				ProgExeLog srartLog = (ProgExeLog)getSession().load(ProgExeLog.class, startLogID);
				qp.removeParameter("startLogID");
				qp.addParameter("operDate", srartLog.getOperDate());
				qp.addParameter("operTime", srartLog.getOperTime());
				sbQueryString.append(" and (l.operDate >:operDate or l.operDate =:operDate and l.operTime >=:operTime)");
			}
			
			return new CommQuery<MetadataCheckLog>().queryListPage(qp, 
					qp.appendOrders(sbQueryString, "l" ), getSession());
		} catch(RuntimeException re) {
			re.printStackTrace();
			return new  ListPage<MetadataCheckLog>();
		}		
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Integer> findMetadataCheckLogPrepLevelIDs(int operDate, int operTime, int prepLevelID) {
		String hql = "select l.prepLevelID from MetadataCheckLog as l where (l.operDate >:operDate or l.operDate =:operDate and l.operTime >=:operTime) " +
				" and l.prepLevelID<=:prepLevelID order by l.operDate,l.operTime";
		Query query = getSession().createQuery(hql);
		query.setInteger("operDate", operDate);
		query.setInteger("operTime", operTime);
		query.setInteger("prepLevelID", prepLevelID);
		return query.list();
	}
	
	@Override
	public int findMetadataCheckLogNum(int startOperDate, int startOperTime, int endOperDate, int endOperTime, int prepLevelID) {
		String hql = "select count(l) from MetadataCheckLog as l where l.prepLevelID<=:prepLevelID" +
				" and (l.operDate >:startOperDate or l.operDate =:startOperDate and l.operTime >=:startOperTime)" +
				" and (l.operDate <:endOperDate or l.operDate =:endOperDate and l.operTime <=:endOperTime)";
		Query query = getSession().createQuery(hql);
		query.setInteger("prepLevelID", prepLevelID);
		query.setInteger("startOperDate", startOperDate);
		query.setInteger("startOperTime", startOperTime);
		query.setInteger("endOperDate", endOperDate);
		query.setInteger("endOperTime", endOperTime);
		Long num = (Long)query.uniqueResult();
		return num.intValue();
	}

}
